#include "stdio.h"
#include "stdlib.h"
#include "assert.h"
typedef int ElemType;

typedef struct SeqList{
    ElemType *data;//data指向数组首地址
    int length;
    int MaxSize;
}SeqList;

void InitList(SeqList* L){
    L->data=(ElemType*)malloc(sizeof (ElemType)*4);
    L->length=0;
    L->MaxSize=4;
}

//进行检查扩容
void SeqListCheckCapacity(SeqList* L){
    if(L->length>=L->MaxSize){
        L->MaxSize *=2;
        L->data = (ElemType*)realloc(L->data,sizeof (ElemType)*L->MaxSize);
    }
}


//尾插
void SeqListPushBack(SeqList* L,ElemType x){
    assert(L);
    if(L->length>=L->MaxSize){
        L->MaxSize *= 2;
        L->data = (ElemType*)realloc(L->data,sizeof (ElemType)*(L->MaxSize));
        if(L->data==NULL){
            printf("扩容失败");
            exit(-1);
        }
    }
    L->data[L->length] = x;
    L->length++;
}

//尾删
void SeqListPopBcak(SeqList* L){
    L->length--;
}

//头插法
void SeqListPushFront(SeqList* L,ElemType x){
    SeqListCheckCapacity(L);
    int end = L->length-1;
    for(int i=end;i>=0;i--){
        L->data[i+1]=L->data[i];
    }
    L->data[0]=x;
    L->length++;
}

//头删
void SeqListPopFront(SeqList* L){
    int start = 0;
    for(int i=0;i<L->length-1;i++){
        L->data[i]=L->data[i+1];
    }
    L->length--;
}

//中间插入
void SeqListInsert(SeqList* L,int pos,ElemType x){
    assert(L);
    assert(pos<=L->length&&pos>=0);
    SeqListCheckCapacity(L);
    int end = L->length-1;
    while (end>=pos){
        L->data[end+1]=L->data[end];
        end--;
    }
    L->length++;
    L->data[pos]=x;
}

//删除某个位置
void SeqListDelete(SeqList* L,int pos){
    int end = L->length-2;
    assert(L);
    assert(pos<L->length&&pos>=0);
    for(int i=pos;i<=end;i++){
        L->data[i]=L->data[i+1];
    }
    L->length--;
}

//查找
int SeqListFind(SeqList *L,ElemType x){
    for(int i=0;i<L->length;i++){
        if(L->data[i]==x){
            return i;
        }
    }
    return -1;
}

//最后销毁
void SeqListDestroy(SeqList* L){
    free(L->data);
    L->data=NULL;
    L->length=0;
    L->MaxSize=0;
}

//打印
void SeqListPrint(SeqList *L){
    for(int i=0;i<L->length;i++){
        printf("%d ",L->data[i]);
    }
    printf("\n");
}

void TestSeqList1(){
    SeqList L;
    InitList(&L);
    SeqListPushBack(&L,1);
    SeqListPushBack(&L,2);
    SeqListPushBack(&L,3);
    SeqListPrint(&L);
    SeqListPopBcak(&L);
    SeqListPrint(&L);
    SeqListPushFront(&L,99);
    SeqListPrint(&L);
    SeqListPopFront(&L);
    SeqListPrint(&L);
    SeqListDestroy(&L);
}



int main(){
    TestSeqList1();
    return 0;
}

